%load alt_calib/eq_mit_mass_shock.mat
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../selection_model/eq_mit_entry_cost.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass
addpath '/CEtools'

set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

Ns = 1 - [7.13]/100;

adj = sum(eq_mit.mu,1);
scx = pick_scale(adj(2:end), Ns(1));

T = 30;

figure(47)

subplot(3,3,1)
hold on
% plot(scx*(eq_mit.C - 1)); drawnow; title('C'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
% subplot(3,3,2)
% hold on
% plot(scx*(eq_mit.D - eq_mit.D(end))); drawnow; title('D'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
 Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);
% 
% subplot(3,3,3)
% hold on
% plot((scx*(total_labor - total_labor(end)))/total_labor(end)); title('Total labor demand'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% 
% subplot(3,3,4)
% hold off
% plot(scx*(eq_mit.entry_rate - eq_mit.entry_rate(end)) + eq_mit.entry_rate(end)); title('Entry rate'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% subplot(3,3,5)
% hold off
% plot(sum(eq_mit.exit.*eq_mit.mu)./sum(eq_mit.mu)); title('Exit rate'); drawnow;
% xlim([1 T]);
% 
% figure(47)
% subplot(3,3,6)
% hold off
% plot(mass); title('Total mass'); drawnow;
% xlim([1 T]);
% 
 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);
% a_prods = repmat(sim.A', glob.Nsf, 1);
% prods = prods.*a_prods;
 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;
% 
 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);
% 
 mu_cw = num./tc;
% 
% 
% figure(47)
% subplot(3,3,7)
% hold off
% plot(scx*(mu_cw - mu_cw(end)) + mu_cw(end)); title('Cost-weighted markup'); xlim([1 T]); drawnow;
% 
% %
% revs = eq_mit.p.*eq_mit.y;
% 
% tr   = sum(revs.*eq_mit.mu)
% num = sum(revs.*eq_mit.mu.*mkps)/tr;
% 
% mu_sw = num./tr;
% 
% subplot(3,3,8)
% hold off
% plot((mu_sw)); title('Sales-weighted markup'); xlim([1 T]); drawnow;
% 
% 
% print('-dpng', 'figures/irfs_entry.png')


Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all


mass = log_linear_scale(adj, scx);
mass = mass/mass(end);

T = 15;
subplot(2,3,1)
plot(mass, 'LineWidth', 3, 'color', 'k'); xlim([1 T]); ylim([.92 1]); title('Mass of estab.')

subplot(2,3,5)
plot(log_linear_scale(eq_mit.C, scx), 'LineWidth', 3, 'color', 'k'); xlim([1 T]); ylim([.95 1]); title('Output')

subplot(2,3,6)
plot(log_linear_scale(eq_mit.W, scx), 'LineWidth', 3, 'color', 'k'); xlim([1 T]); ylim([.95 1]); title('Wage')

subplot(2,3,4)
plot(log_linear_scale(total_labor, scx)/total_labor(end), 'LineWidth', 3, 'color', 'k'); xlim([1 T]); title('Employment')
ylim([.95 1]);

% subplot(2,3,4)
% ngdp = (eq_mit.entry_rate - eq_mit.entry_rate(end))*scx + eq_mit.entry_rate(end);
% plot(ngdp, 'LineWidth', 3, 'color', 'k'); xlim([1 T]);; ylim([.04 .12]); title('Entry rate')

ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

subplot(2,3,2)
labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;



 plot(log_linear_scale(1./labor_share, scx)/(1./labor_share(end)), 'LineWidth', 3, 'color', 'k'); xlim([1 T]); title('Markup'); ylim([1 1.008])
%yyaxis right
%mu_cw_s = log_linear_scale(mu_cw, scx);
%plot(mu_cw_s, 'LineWidth', 3, 'color', 'k');  xlim([1 T]); drawnow;

subplot(2,3,3)
tfp = eq_mit.C'./total_labor;
%tfp = tfp(1:T);
plot(log_linear_scale(tfp, scx)/tfp(end), 'LineWidth', 3, 'color', 'k'); xlim([1 T]);
title('Effective TFP')
ylim([.985 1])

set(gcf,'units','points','position',[10,10,1000,600])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/kimball_entry_cost.png')

%%
%% recompute entry decision & the employment of young firms
addpath ../calibration_v3.5/kimball/
entr = mit_shock_entrants(eq_mit, eq_SS, param, glob, options);

%% plot entry rate, entrant share, and young share
total_mass = log_linear_scale(sum(eq_mit.mu,1), scx);

entr_mass       = log_linear_scale(eq_mit.entry_mass, scx);

exit_mass       = sum(entr.mu.*(entr.exit + param.gamma));
exit_mass       = log_linear_scale(exit_mass, scx);

entr_employment = log_linear_scale(entr.employment_a0, scx);

young_employment = log_linear_scale(entr.employment_a1 + entr.employment_a2 + entr.employment_a3 + entr.employment_a4 + entr.employment_a5, scx);

TL = log_linear_scale(total_labor, scx);

subplot(1,2,1)
plot(entr_mass(1:T)./total_mass(1:T), 'LineWidth', 3, 'color', 'k'); title('Entry Rate')

% add in info from BDS


subplot(1,2,2)
plot(entr_employment(1:T)./TL(1:T), 'LineWidth', 3, 'color', 'k'); title('Emp share - entrants')
ylim([.03 .065])
% add in info from BDS


set(gcf,'units','points','position',[10,10,750,450])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/entrants_cost_shock.png')
